package com.example.myrecycleviewsmoth;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.graphics.Rect;
import android.os.Bundle;
import android.view.View;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {
    private RecyclerView recyclerView;
    private LinearLayoutManager platformLinearLayoutManager;
    private int moveWidth;
    private boolean isUserControl = false;
    private int canMoveWidth;//item的宽度加上旁边的间距
    int marginWidth;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        recyclerView = findViewById(R.id.leke_th_incentive_rv_dress);
        RvAdapter adapter = new RvAdapter(MainActivity.this);
//        platformLinearLayoutManager = new CenterLayoutManager(MainActivity.this, LinearLayoutManager.HORIZONTAL, false);
        platformLinearLayoutManager = new LinearLayoutManager(MainActivity.this);
        platformLinearLayoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);
        recyclerView.setLayoutManager(platformLinearLayoutManager);
        recyclerView.addItemDecoration(new RecyclerItemDecoration(120, 12));
        recyclerView.setAdapter(adapter);

        List<String> mData = new ArrayList<>();
        for (int i = 0; i < 30; i++) {
            mData.add("" + i);
        }
        adapter.setData(mData);


        recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(@NonNull RecyclerView rc, int newState) {
                super.onScrollStateChanged(rc, newState);

                if (newState == RecyclerView.SCROLL_STATE_IDLE) {//滑动停止
                    if (!isUserControl) {
                        smoothScrollToPosition();
                    }
                }
                if (rc.getScrollState() != RecyclerView.SCROLL_STATE_SETTLING) {//非自动滑动
                    isUserControl = false;
                }
            }

            @Override
            public void onScrolled(@NonNull RecyclerView rc, int dx, int dy) {
                super.onScrolled(rc, dx, dy);
                if (rc.getScrollState() == RecyclerView.SCROLL_STATE_SETTLING && !isUserControl) {//自动滚动
                    //滚动幅度 在 -3 .. 3以内 其实时接近停止了 慢速滑动了 这时我们让他停止
                    if (dx < 3 && dx > -3) {//向左右滚动
                        rc.stopScroll();
                    }
                }
            }
        });
    }

    private void smoothScrollToPosition() {
        isUserControl = true;
        View stickyInfoView = recyclerView.getChildAt(0); //获取头部View 第一个view

        //获取view左边不可见的部分宽度 减去recycle view的左边padding 加上旁边的间距
        final int left = stickyInfoView.getRight() - DensityUtils.dp2px(MainActivity.this, 6) + marginWidth;
        int width = stickyInfoView.getWidth();//获取view宽度
        if (left != canMoveWidth) {//去除正好停在正好的位置的情况
            if (left >= (width / 2)) {//判断view在上一半还是在下一半，代表还在上一半然后 往左移

                recyclerView.post(new Runnable() {//放在子线程 防止滑动无效
                    @Override
                    public void run() {
                        recyclerView.smoothScrollBy(-(canMoveWidth - left), 0);//二次滑动
                    }
                });
            } else {
                recyclerView.post(new Runnable() {
                    @Override
                    public void run() {
                        recyclerView.smoothScrollBy(left, 0);//二次滑动
                    }
                });
            }
        }
    }


    public class RecyclerItemDecoration extends RecyclerView.ItemDecoration {

        private int itemWidth;//item的宽度
        private int itemNum;//一屏显示几个item
        private int recyclePadding;//一屏显示几个item

        public RecyclerItemDecoration(int itemWidth, int recyclePadding) {
            this.itemWidth = itemWidth;
            this.recyclePadding = recyclePadding;
        }

        @Override
        public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
            int position = parent.getChildAdapterPosition(view);
            int recycleViewWidth = parent.getWidth();//一屏的recycle View宽度

            itemNum = (int) Math.floor(recycleViewWidth / DensityUtils.dp2px(MainActivity.this, itemWidth));
            int allViewItemWidth = DensityUtils.dp2px(MainActivity.this, itemWidth) * itemNum;//默认一屏显示8个item
            //一屏总宽度 减去 总共可见的item宽度 减去 RecyclerView的水平padding  12
            int userMarginWidth = recycleViewWidth - allViewItemWidth - DensityUtils.dp2px(MainActivity.this, recyclePadding);
            //布满一屏item后 剩余空间用做间距 平分距离作为间距
            marginWidth = userMarginWidth / (itemNum - 1);
            //item的宽度+间距
            canMoveWidth = DensityUtils.dp2px(MainActivity.this, itemWidth) + marginWidth;

            if (position != 0) {
                outRect.left = marginWidth;
            }

        }
    }
}
